package jmine.tec.report.swing;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

import jmine.tec.report.DataColumn;
import jmine.tec.report.DataRow;
import jmine.tec.report.DataTable;

/**
 * {@link AbstractTableModel} for showing {@link DataTable}
 * 
 * @author takeshi
 */
public class ReportTableModel extends AbstractTableModel {

    private final List<DataRow> rows;

    private int cols;

    private NumberFormat nf = NumberFormat.getInstance();

    private NumberFormat cur = NumberFormat.getInstance();

    private DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    private List<String> columnHeader;

    /**
     * @param table {@link DataTable}
     */
    public ReportTableModel(final DataTable table) {
        this.rows = new ArrayList<DataRow>(table.getRows().size());
        this.flatten(table.getRows());
        this.cols = table.getColumns().size() + 1;
        this.columnHeader = new ArrayList<String>(table.getColumns().size());
        for (DataColumn dc : table.getColumns()) {
            this.columnHeader.add(dc.getName());
        }
    }

    /**
     * @param rows2 tows
     */
    private void flatten(final List<? extends DataRow> rows2) {
        for (DataRow r : rows2) {
            this.rows.add(r);
            this.flatten(r.getChildRows());
        }
    }

    /**
     * 
     */
    private static final long serialVersionUID = 8214654844191821442L;

    /**
     * @return int
     */
    public int getColumnCount() {
        return this.cols;
    }

    /**
     * @return int
     */
    public int getRowCount() {
        return this.rows.size();
    }

    /**
     * @param rowIndex int
     * @param columnIndex int
     * @return Object
     */
    public Object getValueAt(final int rowIndex, final int columnIndex) {
        if (columnIndex == 0) {
            return this.rows.get(rowIndex).getNameDataCell().toString(this.df, this.nf, this.nf);
        }
        return this.rows.get(rowIndex).getCell(columnIndex - 1).toString(this.df, this.nf, this.cur);
    }

    /**
     * Creates a JFrame and shows the table for debugging pourposes.
     * 
     * @param table {@link DataTable}
     */
    public static void showFrame(final DataTable table) {
        final JFrame frame = new JFrame("table");
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.getContentPane().setLayout(new BorderLayout());
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(final WindowEvent e) {
                        frame.dispose();
                    }
                });
                JTable jTable = new JTable(new ReportTableModel(table));
                frame.getContentPane().add(new JScrollPane(jTable), BorderLayout.CENTER);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    /**
     * @param column columnIndex
     * @return {@link String}
     */
    @Override
    public String getColumnName(final int column) {
        if (column == 0) {
            return "carteira";
        } else {
            return this.columnHeader.get(column - 1);
        }
    }
}
